import React from 'react';
import { Modal } from 'antd';
import { WeaFormItem } from 'ecCom';
import { WeaSwitch } from 'comsMobx';

export const RenderForm = (form, field, config, renderItem, switchConfig, switchRender = false) => {
  const preCls = 'group-vote-form';
  return (
    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1v0djk`}
      label={`${field.label}`}
      labelCol={{ span: `${field.labelcol}` }}
      wrapperCol={{ span: `${field.fieldcol}` }}
      className={`${preCls}-formItem ${preCls}-${(field.domkey && field.domkey[0]) ? field.domkey[0] : ''}`}
      error={form.getError(field)}
      tipPosition="bottom"
      {...config}
    >
      {
        !switchRender && (
          <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@pdluna`}
            fieldConfig={field}
            form={form}
            {...switchConfig}
          />
        )
      }
      { renderItem }
    </WeaFormItem>
  );
};

export const ValidateForm = (form, callback) => {
  form.validateForm().then((f) => {
    if (f.isValid) {
      if (callback) callback();
    } else {
      f.showErrors();
    }
  });
};

export const FormatDate = (time, str) => {
  let obj = {
    yyyy: time.getFullYear(),
    MM: (`0${time.getMonth() + 1}`).slice(-2),
    dd: (`0${time.getDate()}`).slice(-2),
    HH: (`0${time.getHours()}`).slice(-2),
    mm: (`0${time.getMinutes()}`).slice(-2),
    ss: (`0${time.getSeconds()}`).slice(-2),
  };
  return str.replace(/([a-z]+)/ig, v => obj[v]);
};

export const onCloseHandle = () => {
  if (window.em) {
    em.closeWindow();
  } else {
    window.setDialogFullWindow = false;
    Modal.error({
      transitionName: 'none',
      title: '初始化失败',
      content: 'em对象不存在',
    });
  }
};
